<template>
	<view style="color: #ffa200">
		{{ formater }}
	</view>
</template>

<script>
	export default{
		props:{
			second: {
				type: Number,
				default: 0
			},
			format: {
				type: String,
				default: 'MM-dd hh:mm'
			},
			sformat: {
				type: String,
				default: 'hh:mm:ss'
			},
			suffix: {
				type: String,
				default: ''
			}
		},
		data(){
			return {
				formater:''
			}
		},
		mounted(){
			 this.TIME_FORMAT(this.second)
			//console.log('formater',this.formater)
		},
		methods:{
			// 倒计时逻辑处理
			 startCountdown(ts) {
			     const run = setInterval(() => {
			       if (ts <= 0) {
			         clearInterval(run);
			         this.$emit('counterOver');
			         this.formater = this.TIME_SFORMAT(0, this.sformat, this.suffix);
			       } else {
			         ts -= 1000;
			         this.formater = this.TIME_SFORMAT(ts, this.sformat, this.suffix);
			       }
			     }, 1000);
			   },
			   TIME_FORMAT(ts) {
			     this.startCountdown(ts);
			   },

			TIME_FORMIN (param){
				if (param < 0) {
					param = 0
				}
				if (param < 10) {
					param = '0' + param
				}
				return param
			},
			
			TIME_SFORMAT(ts, sfmt, suffix){
				const time = {
					'h+': this.TIME_FORMIN(Math.floor((ts / 1000 / 60 / 60) % 24)),
					'm+': this.TIME_FORMIN(Math.floor((ts / 1000 / 60) % 60)),
					's+': this.TIME_FORMIN(Math.floor((ts / 1000) % 60))
				}
				for (let k in time) {
					if (new RegExp('(' + k + ')').test(sfmt)) {
						sfmt = sfmt.replace(RegExp.$1, RegExp.$1.length == 1 ? time[k] : ('00' + time[k]).substr(('' + time[k]).length))
					}
				}
				return sfmt + suffix
			}
		}
	}
	
</script>

<style>

</style>